package com.softserveinc.service;

import com.softserveinc.dao.IAuthoritiesDAO;
import com.softserveinc.dao.IUserDao;
import com.softserveinc.model.SaltedUser;
import com.softserveinc.model.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * User Service class
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private IUserDao userDAO;
    @Autowired
    IAuthoritiesDAO authoritiesDAO;

    //get log4j handler
     private static final Logger logger = Logger.getLogger(UserDetailsServiceImpl.class);

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
//        UserDetails user = userDAO.getUserByUsername(username);
        if(logger.isDebugEnabled()){
            logger.debug("Message from loadUserByUsername");
        }

        User user = userDAO.selectUser("newuser");
        List<GrantedAuthority> grantedAuthorities = authoritiesDAO.getUserAuthorities(user);

        return new SaltedUser(user,user.getSalt(),grantedAuthorities,true,true,true,true);
    }
}
